<?php

namespace App\Models\System;

use App\Observers\System\ArticleObserver;
use Dcat\Admin\Admin;
use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class Article extends Model
{
	use HasDateTimeFormatter;

    /**
     * 分类：导航
     */
    const TYPE_NAVIGATION = 'Navigation';

    /**
     * 分类：首页公告
     */
    const TYPE_ANNOUNCEMENT = 'Announcement';

    /**
     * 分类：矿池公告
     */
    const TYPE_ANNOUNCEMENT_KUANGCHI = 'AnnouncementKuang';

    /**
     * 分类：全站弹窗信息
     */
    const TYPE_ALERT_INFO = 'AlertInfo';

    /**
     * 分类： 新闻
     */
    const TYPE_NEWS = 'News';

    /**
     * 分类： 活动
     */
    const TYPE_ACTIVITY = 'Activity';

    /**
     * 分类：帮助中心
     */
    const TYPE_HELPCENTER = 'HelpCenter';
    /**
     * 分类: 公司信息
     */
    const TYPE_APPNEWS = 'AppNews';
    /**
     * 分类: 版本更新
     */
    const TYPE_VERSION = 'Version';

    protected $table = 'yida_articles';

    protected $hidden = ['summary'];

    protected $appends = ['summary'];

    public static function booted()
    {
        parent::booted(); // TODO: Change the autogenerated stub
        static::created(function ($articles){
            $adminId = isset(Admin::user()->id) ? Admin::user()->id : 0;
            $data['admin_id'] = $adminId;
            $data['ip'] = request()->ip();
            $data['url'] = request()->url();
            $data['message'] = ['before_insert'=>$articles->toArray()];
            Log::channel("LaravelInsert")->error("添加操作",[
                'function'=>__CLASS__.'->'.__FUNCTION__.'()',
                'code_line'=>__LINE__,
                'action'=>"insert",
                'message'=>json_encode($data,JSON_UNESCAPED_UNICODE),
                'title' => "添加操作",
                'table' => "yida_articles"
            ]);
        });
        static::updated(function ($articles){
            $adminId = isset(Admin::user()->id) ? Admin::user()->id : 0;
            $data['admin_id'] = $adminId;
            $data['ip'] = request()->ip();
            $data['url'] = request()->url();
            $data['message'] = ['before_update'=>$articles->toArray()];
            Log::channel("LaravelUpdate")->error("更新操作",[
                'function'=>__CLASS__.'->'.__FUNCTION__.'()',
                'code_line'=>__LINE__,
                'action'=>"update",
                'message'=>json_encode($data,JSON_UNESCAPED_UNICODE),
                'title' => "更新操作",
                'table' => "yida_articles"
            ]);
        });
        static::deleted(function ($articles){

            $adminId = isset(Admin::user()->id) ? Admin::user()->id : 0;
            $data['admin_id'] = $adminId;
            $data['ip'] = request()->ip();
            $data['url'] = request()->url();
            $data['message'] = ['before_delete'=>$articles->toArray()];
            Log::channel("LaravelDelete")->error("删除操作",[
                'function'=>__CLASS__.'->'.__FUNCTION__.'()',
                'code_line'=>__LINE__,
                'action'=>"delete",
                'message'=>json_encode($data,JSON_UNESCAPED_UNICODE),
                'title' => "删除操作",
                'table' => "yida_articles"
            ]);
        });

    }


    public function getSummaryAttribute() {
        if(empty($this->attributes['content'])) return '';
        $content=strip_tags($this->attributes['content']);
        $search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript
            "'<[\/\!]*?[^<>]*?>'si", // 去掉 HTML 标记
            "'([\r\n])[\s]+'", // 去掉空白字符
            "'&(quot|#34);'i", // 替换 HTML 实体
            "'&(amp|#38);'i",
            "'&(lt|#60);'i",
            "'&(gt|#62);'i",
            "'&(nbsp|#160);'i"
        ); // 作为 PHP 代码运行
        $replace = array ("","","\\1","\"","&","<",">"," ");
        $content = preg_replace($search, $replace, $content);
        return mb_substr($content,0,255);
    }


}
